package com.example.cunion.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.cunion.common.R;
import com.example.cunion.config.shiro.JwtUtil;
import com.example.cunion.controller.form.*;
import com.example.cunion.entity.Advertisement;
import com.example.cunion.service.AdvertisementService;
import com.example.cunion.util.StringSnowflakeIdGenerator;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/advertisement")
public class AdvertisementController {
    @Resource
    private AdvertisementService advertisementService;
    @Resource
    private JwtUtil jwtUtil;

    @PostMapping("/searchUnCheckAdvertisementByPage")
    public R searchUnCheckAdvertisementByPage(@RequestHeader("token") String token, @RequestBody SearchUnCheckAdvertisementByPageForm form){
        Integer start = form.getStart();
        Integer length = form.getLength();
        start = (start - 1) * length;
        HashMap<String, Object> map = new HashMap<>();
        map.put("start", start);
        map.put("length", length);
        List<HashMap> list = advertisementService.searchUnCheckAdvertisementByPage(map);
        return R.ok().put("result", list);
    }
    @PostMapping("/searchUnCheckAdminAdvertisementByPage")
    public R searchUnCheckAdminAdvertisementByPage(@RequestHeader("token") String token, @RequestBody SearchUnCheckAdminAdvertisementByPageForm form){
        Integer start = form.getStart();
        Integer length = form.getLength();
        start = (start - 1) * length;
        HashMap<String, Object> map = new HashMap<>();
        map.put("start", start);
        map.put("length", length);
        List<HashMap> list = advertisementService.searchUnCheckAdminAdvertisementByPage(map);
        return R.ok().put("result", list);
    }

    @GetMapping("/agreeAdvertisement")
    public R agreeAdvertisement(@RequestHeader("token") String token, @RequestParam("id") String id){
        Integer result = advertisementService.agreeAdvertisement(id);
        return R.ok();
    }

    @GetMapping("/rejectAdvertisement")
    public R rejectAdvertisement(@RequestHeader("token") String token, @RequestParam("id") String id){
        Integer result = advertisementService.rejectAdvertisement(id);
        return R.ok();
    }

    @PostMapping("/addAdvertisement")
    public R addAdvertisement(@RequestHeader("token") String token, @RequestBody AddAdvertisementForm form){
        String userId = jwtUtil.getUserId(token);
        form.setUserId(userId);
        StringSnowflakeIdGenerator generator = new StringSnowflakeIdGenerator(1, 1);
        String id = generator.nextId();
        form.setId(id);
        Map<String, Object> map = BeanUtil.beanToMap(form);
        Integer result = advertisementService.addAdvertisement((HashMap) map);
        return R.ok();
    }

    @PostMapping("/updateAdvertisementStatus")
    public R updateAdvertisementStatus(@RequestHeader("token") String token, @RequestBody UpdateAdvertisementStatusForm form){
        Map<String, Object> map = BeanUtil.beanToMap(form);
        Integer result = advertisementService.updateAdvertisementStatus((HashMap) map);
        return R.ok();
    }


    @PostMapping("/addAdvertisementByMp")
    public R addAdvertisementByMp(@RequestHeader("token") String token, @RequestBody Advertisement advertisement) {
        advertisementService.addAdvertisement(advertisement);
        return R.ok();
    }

    @GetMapping("/deleteAdvertisementByMp")
    public R deleteAdvertisementByMp(@RequestHeader("token") String token, @RequestParam("advertisementId") String advertisementId) {
        advertisementService.deletedAdvertisement(advertisementId);
        return R.ok();
    }
    @PostMapping("/updateAdvertisementByMp")
    public R updateAdvertisementByMp(@RequestHeader("token") String token, @RequestBody Advertisement advertisement) {
        advertisementService.updateAdvertisement(advertisement);
        return R.ok();
    }
    @PostMapping("/searchAdvertisementByMp")
    public R searchAdvertisementByMp(@RequestHeader("token") String token, @RequestBody PageFom form){
        List<Advertisement> list = advertisementService.searchAdvertisement(form.getStart(), form.getLength());
        return R.ok().put("result", list);
    }
}
